// pagesLive/produst/produstList.js
var siteinfo = require("../../siteinfo.js");

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		total: '',
		checked:false,
		checkedAll: false,
		checkedCount: 0,
		produstList: [],
		page: '0',
		produst_id: [],
		no_nore: '0',
		select: false,
		grade_name: '--请选择--',
		grades: [{
			id: 1,
			name: '审核中',
		}, {
			id: 2,
			name: '审核通过'
		},{
			id: 3,
			name: '审核驳回',
		}],
		handieBtn: false,
		delArea: true,
		addGoodstoRoomArea: true,
		status: 1,
		live_roomid: []
	},
	bindShowMsg() {
		this.setData({
			select: !this.data.select
		})
	},
	

	mySelect(e) {
	this.setData({
			delArea: true,
			handieBtn: false,
			addGoodstoRoomArea:true,
			checkedCount: 0,
			checkedAll: false,
			produst_id: []
		})
		console.log(e)
		var name = e.currentTarget.dataset.name
		var id = e.currentTarget.dataset.id

		console.log(id);
		//1审核中，2审核通过，3审核驳回
	
		 if (id === 1) {
			this.setData({
				status: 1
			})
			this.getRoomProdustList();
		} else if (id === 2) {
			this.setData({
				status: 2
			})
			this.getRoomProdustList();
		}else if(id === 3){
			this.setData({
				status: 3
			})
			this.getRoomProdustList();
		}
		this.setData({
			grade_name: name,
			select: false
		})
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		let base_info = wx.getStorageSync('global_baseinfo')
		console.log(base_info.base_color)
		wx.setNavigationBarColor({
			backgroundColor: base_info.base_color,
			frontColor: '#000000',
		})
		console.log(options);
		console.log('app onload')
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function(e) {
		console.log('onShow');
		this.getRoomProdustList();
		this.setData({
			grade_name: '审核中',
			select: false
		})
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},

	//获取的商品列表
	getRoomProdustList: function() {
		wx.showLoading({
			title: '加载中'
		})
		var status = this.data.status
		var $this = this;
		wx.request({
			url: siteinfo.site_url + "api/live/getRoomProdustList",
			data: {
				uniacid: siteinfo.uniacid,
				page: $this.data.page,
				suid: wx.getStorageSync('suid'),
				status: status
			},
			success: function(res) {
				var data = res.data;
				console.log(data)
				if (data.errcode == 0 && data.goods) {
					$this.setData({
						produstList: data.goods,
						total: data.total
					});
					wx.hideLoading({
						complete: (res) => {},
					})
				}
				// if(data.data && data.data.length < 20){
				//     $this.setData({
				//         no_nore: 1
				//     })
				// }
			}
		})
	},
	// 进入商品详情页面
	doProdustInfo: function(e) {
		var that = e;
		var url = that.currentTarget.dataset.url;
		console.log(url);
		wx.navigateTo({
			url: "/" + url
		})
	},

	//进入添加商品
	doProdustAdd: function(e){
		var that = e;
		wx.showLoading({
			title: '载入中',
		})
		wx.navigateTo({
			url: '/pagesLive/produst/produstAdd',
		})
	},

//显示删除区域
showDelArea:function(e){
	console.log(e)

	this.setData({
		delArea: false,
		handieBtn: true
	})
},
showAddGoodsToRoomArea: function(e){
	this.setData({
		addGoodstoRoomArea: false,
		handieBtn: true
	})
},
//checkbox选中事件
checkboxChange:  function (e) {
	console.log('点击选中了')
	 var that = e
	// console.log(that)
	var key = that.currentTarget.dataset.indexid;
	console.log('索引',key)

	this.setData({
		checkedAll: false
	})
	let checkedCount = this.data.checkedCount
	if (that.detail.value != ""){
		this.data.produst_id.push(that.detail.value)
		checkedCount++
		this. setData({
			checkedCount: checkedCount
		})
	 
	}else{
		this.data.produst_id.splice(key);
		console.log('索引',key)
		console.log(false)
		checkedCount--
		this.setData({
			checkedCount: checkedCount
		})
		console.log(this.data.produst_id)
	}


},
//全选
	checkedAll:function (e) {

		let checkedCount = this.data.checkedCount
		console.log(e.detail.value)
		if (e.detail.value.length > 0){
			//全选
			this.data.produstList.forEach((item, index)=>{
				item.checked = true
				console.log('item',item)
					this.data.produst_id.push(item.id)
					checkedCount = this.data.produstList.length
			});

			//取出商品id
			// let productId = this.detailx;
			console.log(this.data);
		}else{
			//取消全选
			this.data.produstList.forEach((item, index)=>{
				item.checked = false
			})
			this.setData({
				produst_id: []
			});
			//清空数量
			checkedCount = 0;
		}
		// console.log(this.data.shop_produstList)
		//设置数据
		this.setData({
			produstList:this.data.produstList,
			checkedCount: checkedCount
		})
	},
//删除直播商品
	delProduct: function (e) {
		var self=this;    
		var  that = e;
		console.log(that);
		wx.showLoading({
			title: '正在删除'
		})
		let data = this.data.produst_id;
		console.log(data);
		if(data == ""){
			wx.hideLoading({
				complete: (res) => {},
			})
			wx.showModal({
				title: '提示',
				content:'请选择商品',
				showCancel: false
			})
			return false
		}
		wx.request({
			url: siteinfo.site_url + 'api/live/delLiveProduct',
			method: 'post',
			header: {
				'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
			},
			data: {
				productid: data,
				uniacid: siteinfo.uniacid,
				suId:wx.getStorageSync('suid')
			},
			success(res) {
				console.log(res.data)
				if(res.data.errcode === 0){
					wx.hideLoading({
						complete: (res) => {},
					})
					wx.showToast({
						title: '删除成功',
						icon: 'success',
						duration: 2000
					})
				setTimeout(() => {
			self.setData({
				delArea: true,
				handieBtn: false,
				checkedCount: 0,
				checkedAll: false,
				produst_id: []
				})
		}, 1500);
					setTimeout(() => {
						self.getRoomProdustList();
					}, 1000);
				}
			}
		})
		
		
	},
	//将商品添加到直播端架
	addGoodsRoom: function (e) {
		var self=this;
		var  that = e;
		console.log(that);
		wx.showLoading({
			title: '正在添加'
		})
		let data = this.data.produst_id;
		console.log(data);
		if(data == ""){
			wx.hideLoading({
				complete: (res) => {},
			})
			wx.showModal({
				title: '提示',
				content:'请选择商品',
				showCancel: false
			})
			return false
		}
		wx.request({
			url: siteinfo.site_url + 'api/live/addLiveGoodsRoom',
			method: 'post',
			header: {
				'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
			},
			data: {
				productid: data,
				uniacid: siteinfo.uniacid,
				suId:wx.getStorageSync('suid')
			},
			success(res) {
				console.log(res.data)
				if(res.data.errcode === 0){
					wx.hideLoading({
						complete: (res) => {},
					})
					wx.showToast({
						title: '添加成功',
						icon: 'success',
						duration: 2000
					})
					setTimeout(() => {
						self.setData({
							addGoodstoRoomArea: true,
							handieBtn: false,
							checkedCount: 0,
							checkedAll: false,
							produst_id: []
						})
					}, 1500);
			
					setTimeout(() => {
						self.getRoomProdustList();
					}, 1000);
				}
				if(res.data.errcode === 300023){
					wx.hideLoading({
						complete: (res) => {},
					})
					wx.showModal({
						title: '提示',
						content:'当前房间状态不允许此操作!',
						showCancel: false
					})
					setTimeout(() => {
						self.setData({
							addGoodstoRoomArea: true,
							handieBtn: false,
							checkedCount: 0,
							checkedAll: false,
							produst_id: []
						})
					}, 1500);
					setTimeout(() => {
						self.getRoomProdustList();
					}, 1000);
				}
			}
		})
	}
})
